/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Creator.java
 *
 * Created on Jul 6, 2010, 3:21:30 PM
 */

package ResultEditor.NewClass;

import java.awt.Color;

/**
 *
 * @author leng
 */
public class Creator extends javax.swing.JFrame {

    protected UserInterface.GUI eHOST_GUI;

    protected Color classcolor;
    /** Creates new form Creator */
    public Creator(UserInterface.GUI eHOST_GUI) {
        this.eHOST_GUI = eHOST_GUI;
        eHOST_GUI.setEnabled(false);
        initComponents();

        jLabel2.setText(null);
        jLabel2.setVisible(false);

        int parent_width = eHOST_GUI.getWidth();
        int parent_height = eHOST_GUI.getHeight();
        int x = eHOST_GUI.getX(), y = eHOST_GUI.getY();
        int width = this.getWidth(), height = this.getHeight();

        int X = x + (int)((parent_width - width)/2);
        int Y = y + (int)((parent_height - height)/2);

        this.setLocation(X, Y);

        jButton_apply.setEnabled(false);

        initColorCombo();


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel3 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jComboBox_color = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jButton_apply = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/markerror.png"))); // NOI18N
        jLabel3.setText("jLabel3");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Build a new annotated class");
        setAlwaysOnTop(true);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setText("Enter your new annotated class name here:");

        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField1KeyTyped(evt);
            }
        });

        jLabel2.setText("jLabel2");

        jLabel4.setText("Designate a display color:");

        jComboBox_color.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox_color.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox_colorActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(20, 20, 20)
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
                            .add(jLabel1)))
                    .add(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabel2))
                    .add(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabel4))
                    .add(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jComboBox_color, 0, 373, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel4)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jComboBox_color, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 84, Short.MAX_VALUE)
                .add(jLabel2)
                .add(34, 34, 34))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel2.setLayout(new java.awt.GridBagLayout());

        jButton_apply.setText("Apply");
        jButton_apply.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_applyActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 217, 2, 0);
        jPanel2.add(jButton_apply, gridBagConstraints);

        jButton2.setText("Cancel");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(4, 3, 2, 8);
        jPanel2.add(jButton2, gridBagConstraints);

        getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void initColorCombo(){

    jComboBox_color.removeAllItems();

    int[] values = new int[] { 0, 128, 192, 255 };
    for (int r = 0; r < values.length; r++)
      for (int g = 0; g < values.length; g++)
        for (int b = 0; b < values.length; b++) {
          if ((r==0)&&(g==0)&&(b==0)) continue;
          Color c = new Color(values[r], values[g], values[b]);
          jComboBox_color.addItem(c);
        }
    jComboBox_color.setRenderer(new ColorComboRenderer());

    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        eHOST_GUI.setEnabled(true);
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton_applyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_applyActionPerformed
        saveNewAnnotatedClassname( jTextField1.getText().trim() );
        eHOST_GUI.setEnabled(true);
        this.dispose();
    }//GEN-LAST:event_jButton_applyActionPerformed

    private void saveNewAnnotatedClassname(String newAnnotatedClass){
        if(( newAnnotatedClass == null)||(newAnnotatedClass.length() < 1 ))
            return ;
        ResultEditor.AnnotationClasses.Depot classdepot = new ResultEditor.AnnotationClasses.Depot();
        classdepot.addElement(newAnnotatedClass.trim(), "User_Built_in_eHOST", this.classcolor, true, false);
        config.project.ProjectConf projectconf = new config.project.ProjectConf(
                    env.Parameters.WorkSpace.CurrentProject);
        projectconf.saveConfigure();

        ResultEditor.SpanEdit.AnnotationClassChooser ac = new ResultEditor.SpanEdit.AnnotationClassChooser();
        ac.applyJustBuiltAnnotatedClass( newAnnotatedClass.trim(), null );
    }

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        eHOST_GUI.setEnabled(true);
    }//GEN-LAST:event_formWindowClosing

    /**while user is typing, check repetitive or not */
    private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyTyped
        jLabel2.setText(null);
        jLabel2.setVisible(false);

        

        String typed = jTextField1.getText().trim() + evt.getKeyChar();

        //System.out.println("keys = [" + typed + "]");

        if (( jTextField1.getText() != null )&&( typed.trim().length() > 0 )){
            if ( repetitiveCheck( typed.trim() )) {
                jButton_apply.setEnabled(false);
                jLabel2.setText(" Annotated classs \"" + typed.trim()  + "\" has existed!!!");
                jLabel2.setIcon( jLabel3.getIcon() );
                jLabel2.setVisible(true);
                
                commons.Tools.beep();

            }else {
                jButton_apply.setEnabled(true);
            }

        }else
            jButton_apply.setEnabled(false);
    }//GEN-LAST:event_jTextField1KeyTyped

    private void jComboBox_colorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox_colorActionPerformed
        int i = jComboBox_color.getSelectedIndex();
        Object entry = jComboBox_color.getSelectedItem();
        if (entry != null){
            classcolor = (Color) entry;
            //System.out.println("color is " + classcolor.toString() );
        }
    }//GEN-LAST:event_jComboBox_colorActionPerformed

    /**check whether an annotated classname has */
    private boolean repetitiveCheck(String newClassname ){       
        if(( newClassname == null)||(newClassname.trim().length() < 1 ))
            return false;
        ResultEditor.AnnotationClasses.Depot classdepot = new ResultEditor.AnnotationClasses.Depot();
        if( classdepot.isExisting(newClassname) )
            return true;
        else
            return false;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton_apply;
    private javax.swing.JComboBox jComboBox_color;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

}
